<?php
include_once('includes.php'); 	

$FeatureClubs = array("Barcelona","Real Madrid",
					"AC Milan","Inter Milan",
					"Bayern Munich",
					"Arsenal","Chelsea","Liverpool","Manchester United","Manchester City"
					);
$AllowCompetition = array(
	"Spanish Primera Division" ,
	"English Premier League",
	"German Bundesliga",
	"Italian Serie A",
	"UEFA Champions League",
	"UEFA Europa League",
	"Friendly Match"
);

$url="http://www.livefootballol.com/live-streaming.html";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$html = curl_exec($ch);

curl_close($ch);


$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$elements = $xpath->query('//table[@class="contentpaneopen"]/tr/td[@valign="top"]/div');

if (!is_null($elements)) {
	$i=0;
	foreach ($elements as $element) 
	{
		if( checkDateFormat($element->nodeValue) )
			$date = $element->nodeValue;
		else
		{
			$competition =  $element->childNodes->item(2)->nodeValue;
			$competition  = trim(preg_replace('/\[|\]/',"", $competition));
			if (!in_array($competition, $AllowCompetition))
				continue;
			
			$kick_off_hour =  trim($element->childNodes->item(1)->nodeValue);
			$url = trim($element->childNodes->item(3)->getAttribute('href'));
			$versus =  trim($element->childNodes->item(3)->nodeValue);
			
			//save match into database---------------------
			$data["competition_id"] = getCompetitionId($competition);
			$data["versus"] = $versus;
			$data["game_week"] = "";
			
			$match_kick_off = formatDate($date);
			$data["kick_off"] =  $match_kick_off["date"]." ".$kick_off_hour;
			$data["timezone"] =  $match_kick_off["timezone"];
			$data["url"] = $url;
			if($url<>"")
				if(saveMatch($data))
					echo $data["kick_off"]." ".$data["timezone"]." ".$competition." ".$data["versus"]." ".$data["url"]."<br/>";
		}		
		
		$i++;
	}
	
}

//=====================================================================

function checkDateFormat($date)
{
	//$date = "Thursday, 22/09/2011 GMT+2";
	//match the format of the date
	if (preg_match ("/^([^.]+)\, ([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([^.]+)$/", $date, $parts))
	{
		//check weather the date is valid of not
        if(checkdate($parts[3],$parts[2],$parts[4]))
			return true;
        else
			return false;
		
  }
  else
    return false;
}

function saveMatch($data)
{
	global $FeatureClubs;
	$sql = "select * from matchs where url='".addslashes($data["url"])."'";
	$matchs=db_fetch_one($sql);
	if($matchs)
		return;
	
	$now = convertTimeToLocal( date("Y-m-d H:i:s") , date("O")/100 );	
	$difference = strtotime(convertTimeToLocal($data["kick_off"],$data["timezone"])) - strtotime($now); 
	$minremain =  round($difference/60);
	
	list($team1, $team2 ) = split('-', $data["versus"] );
	$team1 = trim($team1);
	$team2 = trim($team2);
	$data["team1_id"] = getTeamId( $team1 );
	$data["team2_id"] = getTeamId( $team2 );
	
	if(in_array($team1,$FeatureClubs) || in_array($team2,$FeatureClubs) )
		$data["is_feature"] = 1;
		
	if($minremain <= -150)
		$data["status"] = 2;	//end		
	else if($minremain <= 5)
		$data["status"] = 1;	//now playing	
	else
		$data["status"] = 0;
	
	return db_insert("matchs",$data);
}
function getCompetitionId($competition_name)
{
	$sql = "select id from competition where name='".addslashes($competition_name)."'";
	$competition=db_fetch_one($sql);
	if($competition)
		return $competition->id;
	else
	{
		$data["name"] =  $competition_name;
		return db_insert("competition",$data);
	}
}
function formatDate($date)
{
	//$date = "Thursday, 22/09/2011 GMT+2";
	//match the format of the date
	if (preg_match ("/^([^.]+)\, ([0-9]{2})\/([0-9]{2})\/([0-9]{4}) ([^.]+)$/", $date, $parts))
	{
		$kick_off["date"] = $parts[4]."-".$parts[3]."-".$parts[2];
		$kick_off["timezone"] = str_replace("GMT", "", $parts[5]);
		
	}	
	return $kick_off;
}
?>